package com.yugao.fintech.antelope.shop.model.entity;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.yugao.fintech.antelope.base.model.BasePlusPO;
import com.yugao.fintech.antelope.base.tenant.core.DataIsolation;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品sku
 *

 */
@Getter
@Setter
@Accessors(chain = true)
@TableName(value = "shop_sku_stock", autoResultMap = true)
@DataIsolation(filterMainTenant = true, filterTenant = true, tableName = "shop_sku_stock")
public class SkuStock extends BasePlusPO {
	private Long id;
	
	/**
	 * 商品id
	 */
	private Long goodsId;
	
	/**
	 * sku编号
	 */
	private String skuCode;
	
	/**
	 * 价格
	 */
	private BigDecimal price;
	
	/**
	 * 成本价钱
	 */
	private BigDecimal costPrice;
	
	/**
	 * 库存
	 */
	private Integer stock;
	
	/**
	 * 销量
	 */
	private Integer saleNumber;
	
	/**
	 * 商品销售规格，json格式
	 */
	@TableField(typeHandler = SpecDataHandler.class)
	private List<Spec> specData;
	
	/**
	 * 锁定库存
	 */
	private Integer lockStock;
	
	/**
	 * 商品重量, 默认为kg
	 */
	private BigDecimal weight;
	
	/**
	 * 展示图片
	 */
	private String picture;

	/**
	 * 主租户id(总店)
	 */
	private Long mainTenantId;

	/**
	 * 租户id(门店)
	 */
	private Long tenantId;

    public static LambdaQueryWrapper<SkuStock> lqw() {
		return Wrappers.lambdaQuery();
	}

	@Data
	public static class Spec {
		private String key;
		private String value;
		private Long specId;
	}

	public static class SpecDataHandler extends AbstractJsonTypeHandler<List<Spec>> {

		@Override
		protected List<Spec> parse(String json) {
			return JSON.parseArray(json, Spec.class);
		}

		@Override
		protected String toJson(List<Spec> obj) {
			return JSON.toJSONString(obj);
		}
	}
}
